import io from "socket.io-client";
export default {
  name: "socketio",
  data: function() {
    return {
      socket: undefined
    };
  },
  mounted() {
    this.connection("http://0.0.0.0:9093");
  },
  methods: {
    connection(path) {
      this.socket = io(path);
      this.socket.on("connect", () => {
        if (this.socket.connected) {
          console.log("连接成功");
          this.socket.on("disconnect", () => {
            console.log("user disconnected");
          });
          this.socket.on("chat message", function(msg) {
            console.log("收到消息:" + msg);
          });
        }
      });
    },
    send(message) {
      console.log("获取到子组件消息:" + message);
      this.socket.emit("chat message", {
        content: message,
        sender: "admin"
      });
    }
  }
};
